<?php defined('SYSPATH') or die('No direct script access.');
require_once(APPPATH. 'classes/helpers/url.php');
class Controller_Sandbox extends Controller {
	private $sandbox_user = 'admin';
	private $sandbox_pswd = '@am1n@c0upleb00k';
	
	public function action_index() {
		if($this->checkAuth()) {
			$email = Arr::get($_POST, 'email');
			$view = View::factory('sandbox');
			if($email == '') {
				echo $view;
				return;
			}
			require_once(APPPATH. 'classes/model/Model_UserLogin.php');
			$ul = new Model_UserLogin();
			$uid = $ul->getUidByEmail($email, Model_UserLogin::NETWORK_SELF);
			if($uid == 0) {
				$view->msg = "$email not found on database.";
				echo $view;
				return;
			}
			$status = $ul->deleteUser($uid);
			if($status < 1) $view->msg = "Error: User not deleted";
			else if($status > 1) $view->msg = "Error: Some other users might be affeceted";
			else $view->msg = "User deleted successfully";
			echo $view;
			return;
		}
	}
	private function checkAuth() {
		$user = Arr::get($_SERVER, 'PHP_AUTH_USER', '');
		$pswd = Arr::get($_SERVER, 'PHP_AUTH_PW', '');
		if($user != $this->sandbox_user || $pswd != $this->sandbox_pswd)
		{
			$this->response->headers('WWW-Authenticate', 'Basic realm="Admistrator area"');
			$this->response->headers('HTTP/1.0', '401 Unauthorized');
			//header("WWW-Authenticate: Basic realm=\"Admistrator area\"");
			//header("HTTP/1.0 401 Unauthorized");
			echo <<<HTML
        	<html><body>
        	<h1>Welcome to admin area!</h1>
        	<big>Authorise to get access!</big><br/>&nbsp;<br/>&nbsp;
        	</body></html>
HTML;
			return false;
		} else {
			return true;
		}
	}
}